<template>
  <div class="drawerComment">
    <el-drawer :visible="drawComment"
               :title="'评论数' + '(' + total + ')'"
               size="40%"
               :before-close="handleClose"
               ref="drawer">
      <!-- 评论列表 -->

      <div v-if="commentData.length">
        <div class="commentList"
             v-for="(item, i) in commentData"
             :key="i">
          <img class="avaterImg"
               :src="item.user.avatar"
               alt="" />
          <div class="commentInfo">
            <div class="nickName">
              <span style="color: #000; font-weight: 400">{{
                item.user.nickname
              }}</span>
            </div>
            <div class="rate">
              <el-rate v-model="item.score"
                       disabled
                       text-color="#ff9900"
                       score-template="{value}">
              </el-rate>
            </div>
            <div class="textComment"> {{ item.technicianEvaluation }} </div>

            <div class="commentTag">
              <el-tag v-for="(tag, i) in item.evaluationKeywords"
                      :key="i"
                      type="success"
                      icon="el-icon-delete">{{ tag }}</el-tag>
            </div>

            <div class="commentfooter">
              <div class="commentTime"> {{ formattedDate(item.createdAt) }} </div>
              <div @click="deletedFn(item.id)"
                   class="deleted"> 删除 </div>
            </div>
          </div>
        </div>
      </div>
      <div v-else><el-empty description="暂无评论"></el-empty></div>
    </el-drawer>
  </div>
</template>

<script>
import moment from 'moment'
import { deleteCommment } from '@/api/technicianManagement/technicianList/index.js';
export default {
  name: 'drawerComment',
  props: {
    drawComment: {
      type: Boolean,
      default: false
    },
    data: {
      type: Object,
      default: () => { }
    }
  },
  components: {},

  data () {
    return {
      value: 5,
      commentData: null,
      total: null
    };
  },
  methods: {
    // 关闭抽屉
    handleClose () {
      console.log('评论');
      this.$emit('update:drawComment', false);
    },
    async deletedFn (id) {
      console.log(id);
      const res = await deleteCommment(id);

      this.$message.success(res);
      this.onload();
    },
    async onload () {
      this.handleClose()
      //   const res = await getByTechnicianComments({
      //     technicianId: row.id,
      //     siteId: this.$store.state.site.info.id,
      //     page: 1,
      //     limit: 10
      //   });
      //   this.commentData = res;
    }
  },
  computed: {
    formattedDate () {
      return (params) => {
        const currentDate = moment(params);
        const formattedDate = currentDate.format('YYYY-MM-DD HH:mm');
        return formattedDate;
      };
    }
  },
  watch: {
    drawComment (visible) {
      this.$nextTick(() => {
        if (visible) {
          if (this.data) {
            // 隐藏账号密码
            console.log(this.data);

            this.data.list.forEach((item) => {
              const imgStr = item.evaluationKeywords;
              if (imgStr.length) {
                if (imgStr.includes(',')) {
                  console.log('字符串中包含逗号');
                  // 包含的进行slice(',') 分割
                  item.evaluationKeywords = imgStr
                    .split(',')
                    .map((item) => item.trim());
                  if (item.evaluationKeywords[item.evaluationKeywords.length - 1] == '') {
                    item.evaluationKeywords.pop()
                  }
                } else {
                  console.log('字符串中不包含逗号');
                  item.evaluationKeywords = imgStr.split();
                  if (item.evaluationKeywords[item.evaluationKeywords.length - 1] == '') {
                    item.evaluationKeywords.pop()
                  }
                }
              }
            });
            console.log(this.data.list);
            (this.commentData = this.data.list),
              console.log(this.commentData);
            this.total = this.data.count;
          } else {
            console.log(this.data);
          }
        } else {
        }
      });
    }
  }
};
</script>
<style lang="less">
.drawerComment {
  .commentList {
    display: flex;
    padding: 14px 0;
    border-bottom: 1px solid #d9d9d9;
    margin: 0 20px;
    .avaterImg {
      width: 46px;
      height: 46px;
      border-radius: 46px;
      margin-right: 10px;
    }
    .commentInfo {
      width: 100%;
      .genderMask {
        width: 40px;
        height: 16px;
        background-color: #fd73a5;
        font-size: 12px;
        font-weight: 400;
        border-radius: 22px;
        display: inline-block;
        color: #fff;
        text-align: center;
        padding-right: 6px;
        line-height: 16px;
        margin-left: 10px;
        &::before {
          content: url('../../../../static/svg/girl.svg');
          display: inline-block;
          vertical-align: middle;
        }
      }
      .rate {
        margin: 5px 0;
      }

      .commentTime {
        color: #999;
        font-size: 13px;
        font-weight: 400;
      }
      .textComment {
        color: #000;
        margin: 10px 0;
        font-size: 14px;
        width: 85%;
      }
      .commentTag {
        margin: 8px 0;
      }
      .commentfooter {
        display: flex;
        justify-content: space-between;
        align-items: center;
        .deleted {
          margin-left: 15px;
          cursor: pointer;
          &::before {
            content: url('../../../../static/svg/waste.svg');
            display: inline-block;
            vertical-align: middle;
          }
        }
      }
    }
  }
}
</style>
